﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class ContractTransfer: IContractTransfer
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public ContractTransfer(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.ContractTransfer GetContractTransfer(System.Int32 contractTransferID)
		{
			BEGDC.PreContract.DAL.ContractTransfer contractTransfer = BEGDC.PreContract.DAL.ContractTransfer.TryFind(contractTransferID);

            return contractTransfer;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetContractTransfer_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM ContractTransfer";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteContractTransfer(System.Int32 contractTransferID)
		{
			try
            {
				BEGDC.PreContract.DAL.ContractTransfer contractTransfer = BEGDC.PreContract.DAL.ContractTransfer.TryFind(contractTransferID);
                contractTransfer.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ContractTransfer", string.Format("Error occured when deleting the ContractTransfer, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddContractTransfer(Nullables.NullableDateTime transferDate, System.Double transferMoney, System.String contractNo, BEGDC.PreContract.DAL.PreContractInfo preContractID, Nullables.NullableDateTime updateDate, System.String transferType, System.Double remainMoney)	
		{
			try
            {
				BEGDC.PreContract.DAL.ContractTransfer contractTransfer = new BEGDC.PreContract.DAL.ContractTransfer();
				
				contractTransfer.TransferDate = transferDate;
				contractTransfer.TransferMoney = transferMoney;
				contractTransfer.ContractNo = contractNo;
				contractTransfer.PreContractID = preContractID;
				contractTransfer.UpdateDate = updateDate;
				contractTransfer.TransferType = transferType;
				contractTransfer.RemainMoney = remainMoney;
				contractTransfer.CreateAndFlush();
				
				return contractTransfer.ContractTransferID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ContractTransfer", string.Format("Error occured when adding a new ContractTransfer, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateContractTransfer(System.Int32 contractTransferID, Nullables.NullableDateTime transferDate, System.Double transferMoney, System.String contractNo, BEGDC.PreContract.DAL.PreContractInfo preContractID, Nullables.NullableDateTime updateDate, System.String transferType, System.Double remainMoney)
		{
			try
            {
                BEGDC.PreContract.DAL.ContractTransfer contractTransfer = GetContractTransfer(contractTransferID);

				contractTransfer.TransferDate = transferDate;
				contractTransfer.TransferMoney = transferMoney;
				contractTransfer.ContractNo = contractNo;
				contractTransfer.PreContractID = preContractID;
				contractTransfer.UpdateDate = updateDate;
				contractTransfer.TransferType = transferType;
				contractTransfer.RemainMoney = remainMoney;
				contractTransfer.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ContractTransfer", string.Format("Error occured when updating the ContractTransfer, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



